package com.atguigu.gmall.ums.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.ums.mapper.UserMapper;
import com.atguigu.gmall.ums.entity.UserEntity;
import com.atguigu.gmall.ums.service.UserService;
import org.springframework.util.CollectionUtils;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<UserEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<UserEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public Boolean chechData(String data, Integer type) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        switch (type){
            case 1: queryWrapper.eq("username",data);break;
            case 2: queryWrapper.eq("phone",data);break;
            case 3: queryWrapper.eq("email",data);break;
            default:
                return false;
        }
        return this.count(queryWrapper)==0;
    }

    /*
    注册
     */
    @Override
    public void register(UserEntity userEntity, String code) {
        //生成盐
        String substring = StringUtils.substring(UUID.randomUUID().toString(), 0, 6);
        userEntity.setSalt(substring);
        String password = userEntity.getPassword();
        //mds加密
        String md5Hex = DigestUtils.md5Hex(password+substring);
        userEntity.setPassword(md5Hex);
        userEntity.setCreateTime(new Date());
        userEntity.setLevelId(1l);
        userEntity.setSourceType(1);
        userEntity.setIntegration(1000);
        userEntity.setGrowth(1000);
        userEntity.setStatus(1);
        this.save(userEntity);
    }

    //登录
    @Override
    public UserEntity queryUser(String loginName, String password) {
        //判断用户名是否重复
        List<UserEntity> list = this.list(new QueryWrapper<UserEntity>().eq("username", loginName).
                or().eq("phone", loginName).
                or().eq("email", loginName));
        if (CollectionUtils.isEmpty(list)){
            throw new RuntimeException("用户名或密码错误");
        }
        //判断密码是否正确
        for (UserEntity userEntity : list) {
            if (StringUtils.equals(userEntity.getPassword(), DigestUtils.md5Hex(password+userEntity.getSalt()))){
                return userEntity;
            }
        }
        throw new RuntimeException("用户名或密码错误");
    }

}